package com.atguigu.jxc.service.impl;/*
 *@author 尹凯瑞
 *@date 2020/12/3 11:30
 */

import com.atguigu.jxc.dao.CustomerDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerDao customerDao;


    //客户列表分页查询
    @Override
    public Map<String, Object> listCustomer(Integer page, Integer rows, String customerName) {
        //创建集合保存前端需要的信息
        Map<String,Object> map = new HashMap<>();
        //当前页
        page = page == 0 ? 1 : page;
        //每页数据从哪儿开始
        int offSet = (page - 1) * rows;
        //查询客户列表
        List<Customer> customerList = customerDao.listCustomer(offSet,rows,customerName);
        //封装数据到集合
        map.put("rows",customerList);
        map.put("total",customerDao.getCustomerCount(customerName));
        //返回
        return map;
    }
    //客户添加或修改
    @Override
    public ServiceVO saveOrUpdate(Customer customer) {
        if(customer.getCustomerId() != null){
            customerDao.updateCustomer(customer);
        }else{
            customerDao.saveCustomer(customer);
        }
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }
    //删除客户
    @Override
    public ServiceVO deleteCustomer(String ids) {
        String[] split = ids.split(",");
        customerDao.deleteCustomer(split);
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }
}
